

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.RegisteredUser;

import sessionbeans.SearchBeanRemote;

/**
 * Servlet implementation class SearchServlet
 */
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String searchedName,type,ability, userId = (String) request.getSession().getAttribute("userId");
		InitialContext context;
		List<RegisteredUser> users;
		
		searchedName = (String) request.getParameter("searchedname");
		type = (String) request.getParameter("type");
		ability = (String) request.getParameter("ability");
		
		if (type!=null && userId != null){
			if ((type.equals("name") && searchedName != null)|| (!(type.equals("name")) && ability!=null)) {
				try {
					context = new InitialContext();
					Object ref = context.lookup("SearchBeanRemote");
					SearchBeanRemote searchBean = (SearchBeanRemote)PortableRemoteObject.narrow(ref, SearchBeanRemote.class);
				
					if (type.equals("search on friends")) {
						users = searchBean.searchOnFriends(userId, ability);
						eliminateUserFromResults(userId,users);
						request.getSession().setAttribute("result",users);
					}
					else if (type.equals("search on all")) {
						users = searchBean.searchOnAll(ability);
						eliminateUserFromResults(userId,users);
						request.getSession().setAttribute("result",users);
					}
					else {
						users = searchBean.searchByName(searchedName);
						eliminateUserFromResults(userId,users);
						request.getSession().setAttribute("result",users);
					}
					response.sendRedirect("SearchPage.jsp");
				} catch (NamingException e) {
					System.out.println("Naming exception");
					e.printStackTrace();
				}
			}
			else {
				response.sendRedirect("SearchPage.jsp");
			}		
		}
		else {
			response.sendRedirect("SearchPage.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private void eliminateUserFromResults(String userId,List<RegisteredUser> users) {
		RegisteredUser toBeEliminated = null;
		
		for (RegisteredUser ru: users) {
			if (ru.getUserId().equals(userId)) {
					toBeEliminated = ru;
					break;
			}
		}
		if (toBeEliminated != null) {
			users.remove(toBeEliminated);
		}
	}
}